package cc.deng.wubo.minapp.controller;

import cc.deng.wubo.minapp.beans.FileUploadInfo;
import cc.deng.wubo.minapp.beans.game.GmIdiom;
import cc.deng.wubo.minapp.core.page.Pagination;
import cc.deng.wubo.minapp.service.FileUploadService;
import cc.deng.wubo.minapp.service.IdiomService;
import cc.deng.wubo.minapp.utils.StringUtil;
import org.nutz.Nutz;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.filepool.NutFilePool;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;

@Controller
@RequestMapping("/idiom")
@SuppressWarnings("all")
public class IdiomController {

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private NutFilePool tempFilePool;

    @Autowired
    private IdiomService idiomService;
    @Autowired
    Dao dao;



    @RequestMapping("/del")
    @ResponseBody
    public Object del(Long id){
        NutMap result = new NutMap();

        try {
            // 删除记录关联的文件\
            GmIdiom idiom = idiomService.fetch(id);
            Long fid = idiom.getFid();
            fileUploadService.deleteFile(fid);
            idiomService.delete(id);
            result.put("success",true);

        }catch (Exception e){
            result.put("success",false);
            result.put("errorInfo","操作失败!");


        }
        return result;
    }




    @RequestMapping("/list")
    @ResponseBody

    public Object list( @RequestParam(value = "page",required = false)Integer page,
                        @RequestParam(value = "rows",required = false)Integer rows){
        NutMap resultMap = new NutMap();
        Pagination pagination = idiomService.listPage(page, rows, Cnd.where(null));
        List<?> list = pagination.getList();
        int total = pagination.getTotalCount();

        resultMap.put("rows",list);
        resultMap.put("total",total);

        return resultMap;

    }


    @RequestMapping("/save")
    @ResponseBody
    public Object save(GmIdiom idiom,MultipartFile[] file) {
        NutMap map = new NutMap();
        List<String> results = new ArrayList<>();
        try {
            for (MultipartFile ft : file) {
                byte[] bytes = ft.getBytes();
                String suffix = StringUtil.suffix(ft);
                File tf = tempFilePool.createFile(suffix);
                OutputStream out = new FileOutputStream(tf);
                out.write(bytes);
                out.flush();
                out.close();
                System.out.println(tf.getPath());
                results.add(tf.getPath());
            }
            List<FileUploadInfo> upload = fileUploadService.upload(results);
            idiom.setFid(upload.get(0).getId());
            List<String> names = Arrays.asList(idiom.getName().split(""));


            Set<String> assistList = StringUtil.getAssistList(names, StringUtil.namesLib(), 20, 20, new HashSet<>());

            List<String> tempNames = new ArrayList<>();
            tempNames.addAll(names);
            tempNames.addAll(assistList);
            names = new ArrayList<>();
            names.addAll(tempNames);
            Collections.shuffle(names);


            StringBuilder sb = new StringBuilder();
            names.forEach(name->{
                sb.append(name);
            });

            idiom.setShuffle(sb.toString());


           // idiom.setId(null);
            GmIdiom insert = dao.insert(idiom);
            map.put("success",true);
            map.put("data",insert);

        }catch (Exception e){
            e.printStackTrace();
        }


        return map;
    }

}
